﻿using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Wallpaper.Models.Entities;

namespace Wallpaper.Data.Configurations
{
    public class WallpaperAndTagConfigration : IEntityTypeConfiguration<WallpaperTag>
    {
        public void Configure(EntityTypeBuilder<WallpaperTag> builder)
        {
            builder.ToTable("wallpaper_tags");

            builder.HasKey(wat => wat.Id);
            builder.Property(wat => wat.Id)
                .HasMaxLength(36)
                .IsRequired()
                .HasComment("关联表主键ID");

            builder.Property(wat => wat.WallpaperId)
                .HasMaxLength(36)
                .IsRequired()
                .HasComment("壁纸ID");

            builder.Property(wat => wat.TagId)
                .HasMaxLength(36)
                .IsRequired()
                .HasComment("标签ID");



            // 🔥 关键：添加复合唯一约束，防止重复关联
            builder.HasIndex(wat => new { wat.WallpaperId, wat.TagId })
                .IsUnique()
                .HasDatabaseName("idx_wallpaper_tag_unique");

            // 添加查询索引
            builder.HasIndex(wat => wat.WallpaperId)
                .HasDatabaseName("idx_wallpaper_tag_wallpaper");

            builder.HasIndex(wat => wat.TagId)
                .HasDatabaseName("idx_wallpaper_tag_tag");
        }
    }
}
